package cn.iocoder.yudao.module.social.dal.mysql.container;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.social.controller.admin.container.vo.ContainerPageReqVO;
import cn.iocoder.yudao.module.social.dal.dataobject.container.ContainerDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 容器 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ContainerMapper extends BaseMapperX<ContainerDO> {

    default PageResult<ContainerDO> selectPage(ContainerPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ContainerDO>()
                .eqIfPresent(ContainerDO::getType, reqVO.getType())
                .likeIfPresent(ContainerDO::getName, reqVO.getName())
                .eqIfPresent(ContainerDO::getParentId, reqVO.getParentId())
                .eqIfPresent(ContainerDO::getPermission, reqVO.getPermission())
                .eqIfPresent(ContainerDO::getCreatorUserId, reqVO.getCreatorUserId())
                .betweenIfPresent(ContainerDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(ContainerDO::getSort)
                .orderByDesc(ContainerDO::getId));
    }

    default List<ContainerDO> selectListByType(String type) {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getType, type)
                .orderByAsc(ContainerDO::getSort)
                .orderByDesc(ContainerDO::getCreateTime));
    }

    default List<ContainerDO> selectListByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getParentId, parentId)
                .orderByAsc(ContainerDO::getSort)
                .orderByDesc(ContainerDO::getCreateTime));
    }

    default List<ContainerDO> selectListByParentIdIsNull() {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .isNull(ContainerDO::getParentId)
                .orderByAsc(ContainerDO::getSort)
                .orderByDesc(ContainerDO::getCreateTime));
    }

    default ContainerDO selectByName(String name) {
        return selectOne(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getName, name));
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getParentId, parentId));
    }

    default Long selectCountByType(String type) {
        return selectCount(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getType, type));
    }

    /**
     * 根据创建者查询容器列表
     *
     * @param creator 创建者
     * @return 容器列表
     */
    default List<ContainerDO> selectListByCreator(String creator) {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getCreator, creator)
                .orderByDesc(ContainerDO::getCreateTime));
    }

    /**
     * 根据父容器编号和类型查询容器列表
     *
     * @param parentId 父容器编号
     * @param type 容器类型
     * @return 容器列表
     */
    default List<ContainerDO> selectListByParentIdAndType(Long parentId, String type) {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .eq(ContainerDO::getParentId, parentId)
                .eqIfPresent(ContainerDO::getType, type)
                .orderByAsc(ContainerDO::getSort));
    }

    /**
     * 根据类型查询公开容器列表
     * 这里定义公开容器为：系统创建的容器（creator为system或admin）
     *
     * @param type 容器类型
     * @return 公开容器列表
     */
    default List<ContainerDO> selectPublicListByType(String type) {
        return selectList(new LambdaQueryWrapperX<ContainerDO>()
                .eqIfPresent(ContainerDO::getType, type)
                .in(ContainerDO::getCreator, "system", "admin", "1") // 系统管理员创建的容器
                .orderByAsc(ContainerDO::getSort));
    }

}
